package community.hospital.service.application.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.common.RedisConstant;
import community.hospital.entity.request.ListOrderRequest;
import community.hospital.entity.request.OrderRequest;
import community.hospital.entity.response.OrderResponse;
import community.hospital.service.application.AnalyseApplicationService;
import community.hospital.service.application.OrderApplicationService;
import community.hospital.util.RedisUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import static community.hospital.common.RedisConstant.PER_DAY_VISITS;
import static community.hospital.common.RedisConstant.SPLIT;

/**
 * @Author：ydj
 * @Date：2023/3/12 16:10
 */
@Service
@AllArgsConstructor
public class AnalyseApplicationServiceImpl implements AnalyseApplicationService {
    private final RedisUtil redisUtil;

    private final OrderApplicationService orderApplicationService;

    @Override
    public List<Integer> trafficTrends() {

        Map<Object,Object> map = redisUtil.getHash(RedisConstant.PER_HOUR_VISITS);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            String hashKey = LocalDate.now() + SPLIT + i;
            Integer o = (Integer) map.get(hashKey);
            if (o == null) {
                list.add(0);
            }else {
                list.add(o);
            }
        }
        return list;

    }


    @Override
    public List<Integer> visits() {
        LocalDate now = LocalDate.now();
        Map<Object, Object> map = redisUtil.getHash(PER_DAY_VISITS);
        //获取当前月份的天数
        Calendar instance = Calendar.getInstance();
        int actualMaximum = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
        ArrayList<Integer> integers = new ArrayList<>();
        String hashKey = now.toString();
        String[] split = hashKey.split("-");
        String prefix = split[0] + "-" + split[1]+"-";
        for (int i = 1; i <= actualMaximum; i++) {
            hashKey = i < 9 ? prefix + 0 + i : prefix + i;
            Integer o = (Integer) map.get(hashKey);
            integers.add(o);
        }
        return integers;
    }

    @Override
    public long countPrice() {
        Page<OrderResponse> orderResponsePage = getOrderResponsePage();
        AtomicInteger atomicInteger = new AtomicInteger(0);
        orderResponsePage.getRecords().forEach(o->{
            if (ObjectUtil.isNotNull(o.getPrice())) {
                atomicInteger.addAndGet(o.getPrice());
            }
        });
        return atomicInteger.get();
    }

    private Page<OrderResponse> getOrderResponsePage() {
        ListOrderRequest request = new ListOrderRequest();
        Calendar calendar = Calendar.getInstance();
        int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        request.setStartTime(calendar.getTime());
        calendar.set(Calendar.DAY_OF_MONTH, actualMaximum);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND, 59);
        request.setEndTime(calendar.getTime());
        request.setPageNumber(1);
        request.setPageSize(Integer.MAX_VALUE);
        Page<OrderResponse> orderResponsePage = orderApplicationService.listOrder(request);
        return orderResponsePage;
    }

    @Override
    public long countVolume() {
       return getOrderResponsePage().getRecords().size();

    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        System.out.println(LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault()));
        calendar.set(Calendar.DAY_OF_MONTH, actualMaximum);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND, 59);
        System.out.println(LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault()));
    }
}
